//
// Created by 86155 on 2025/4/16.
//

#ifndef LLVM_FEISELLOWERING_H
#define LLVM_FEISELLOWERING_H
#include "llvm/CodeGen/TargetLowering.h"

namespace llvm {
class FeSubtarget;
namespace FeISD {
enum NodeType : unsigned { FIRST_NUMBER = ISD::BUILTIN_OP_END, RET_GLUE,STO,LOA,Call};
}
class FeTargetLowering : public TargetLowering{
  const FeSubtarget &Subtarget;
public:
  explicit FeTargetLowering(const TargetMachine &TM, const FeSubtarget &STI);

  const FeSubtarget &getSubtarget() const { return Subtarget; }

  //入参
  SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
                               bool IsVarArg,
                               const SmallVectorImpl<ISD::InputArg> &Ins,
                               const SDLoc &DL, SelectionDAG &DAG,
                               SmallVectorImpl<SDValue> &InVals) const override;

  //返回
  SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
                      const SmallVectorImpl<ISD::OutputArg> &Outs,
                      const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
                      SelectionDAG &DAG) const override;

  const char *getTargetNodeName(unsigned Opcode) const override;     //告诉后端节点名称

  SDValue LowerCall(CallLoweringInfo &CLI, SmallVectorImpl<SDValue> &InVals) const override;

  SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;

  void addx(){
    x++;
  }

private:
  SDValue LowerLOAD(SDValue Op,SelectionDAG &DAG) const;
  SDValue LowerSTORE(SDValue Op,SelectionDAG &DAG) const;
  SDValue LowerCopyToReg(SDValue Op,SelectionDAG &DAG) const;
  int x = 0;
};
}

#endif // LLVM_FEISELLOWERING_H
